package com.dshore.messagecenter.service.warning.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dshore.messagecenter.dao.warning.WarningRecordDao;
import com.dshore.messagecenter.model.statistics.MrKPIRecordCurrent;
import com.dshore.messagecenter.model.topicqueue.MdTopic;
import com.dshore.messagecenter.model.warning.MrWarningProcessing;
import com.dshore.messagecenter.model.warning.MrWarningThreshold;
import com.dshore.messagecenter.service.warning.WarningRecordService;
import com.dshore.messagecenter.util.UUIDUtil;
@Service
public class WarningRecordServiceImpl implements WarningRecordService{
	@Autowired
	WarningRecordDao warningRecordDao;
	/**
	 * 检测
	 */
	public void fiveMinuteWarning() {
		String timeCode=warningRecordDao.selectTimeCode("5分钟");
		//5分钟阈值表数据
		List<MrWarningThreshold>list=warningRecordDao.selectThreshold(timeCode);
		detection(list);
	}

	/**
	 * 十分钟预警
	 */
	public void tenMinuteWarning() {
		String timeCode=warningRecordDao.selectTimeCode("10分钟");
		//10分钟阈值表数据
		List<MrWarningThreshold>list=warningRecordDao.selectThreshold(timeCode);
		detection(list);
	}
/**
 * 检测
 */
	public void detection(List<MrWarningThreshold>list) {
		if(list!=null) {
			for(MrWarningThreshold threshold:list) {
				String objType=threshold.getObjType();
				//区分阈值类型  主题队列和消息队列
				if("1701".equals(objType)) {
					//主题队列
					String kpi=threshold.getKpiId();
					if("1603".equals(kpi)) {
						//消息堆积  
						//需找出单个队列进行统计
						//找出所有未删除已发布的主题
						List<MdTopic>topics=warningRecordDao.selectTopic("1302");
						if(topics!=null) {
							for(MdTopic topic:topics) {
								//找出单个主题队列下所有的队列
								List<String>accesses=warningRecordDao.selectTopicAccess(topic.getTopicQueueNo());
								List<String>sends=warningRecordDao.selectTopicSend(topic.getTopicQueueNo());
								List<MrKPIRecordCurrent>kpis=warningRecordDao.selectMessage(accesses,sends);
								double kpiValue = 0;
								if(kpis!=null) {
									for(MrKPIRecordCurrent kpiCurrent:kpis) {
										kpiValue+=Double.valueOf(kpiCurrent.getKpiValue());
									}
									match(threshold,kpi,topic.getId(),kpiValue);
								}
							}
						}
					}else if("1604".equals(kpi)) {
						//内存占用
						//需找出单个队列进行统计
						//找出所有未删除已发布的主题
						List<MdTopic>topics=warningRecordDao.selectTopic("1302");
						if(topics!=null) {
							for(MdTopic topic:topics) {
								//找出单个主题队列下所有的队列
								List<String>accesses=warningRecordDao.selectTopicAccess(topic.getTopicQueueNo());
								List<String>sends=warningRecordDao.selectTopicSend(topic.getTopicQueueNo());
								List<MrKPIRecordCurrent>kpis=warningRecordDao.selectMessage(accesses,sends);
								double kpiValue = 0;
								if(kpis!=null) {
									for(MrKPIRecordCurrent kpiCurrent:kpis) {
										kpiValue+=Double.valueOf(kpiCurrent.getKpiValue());
									}
									//内存占用需进行单位换算
									//查询单位
									String unitCode=warningRecordDao.selectUnitName(threshold.getUnitCode());
									if("GB".equals(unitCode)) {
										//GB
										kpiValue=kpiValue/1024/1024/1024;
										BigDecimal b=new BigDecimal(kpiValue);
										kpiValue=b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
									}else {
										//MB
										kpiValue=kpiValue/1024/1024;
										BigDecimal b=new BigDecimal(kpiValue);
										kpiValue=b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
									}
									match(threshold,kpi,topic.getId(),kpiValue);
								}

							}
						}
					}else if("1601".equals(kpi)||"1602".equals(kpi)){
						//消息流量与平均延迟
						List<MrKPIRecordCurrent>kpis=warningRecordDao.selectMessage(kpi,threshold.getObjType());
						if(kpis!=null) {
							for(MrKPIRecordCurrent kpiCurrent:kpis) {
								match(threshold,kpiCurrent.getKpiCode(),kpiCurrent.getObj(),Double.valueOf(kpiCurrent.getKpiValue()));
							}
						}
					}
				}else if("1703".equals(objType)){
					//消息队列
					//内存占用，磁盘剩余空间
					List<MrKPIRecordCurrent>kpis=warningRecordDao.selectMessage(threshold.getKpiId(),threshold.getObjType());
					if(kpis!=null) {
						for(MrKPIRecordCurrent kpiCurrent:kpis) {
							double kpiValue=Double.valueOf(kpiCurrent.getKpiValue());
							String unitCode=warningRecordDao.selectUnitName(threshold.getUnitCode());
							if("GB".equals(unitCode)) {
								//GB
								kpiValue=kpiValue/1024/1024/1024;
								BigDecimal b=new BigDecimal(kpiValue);
								kpiValue=b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
							}else {
								//MB
								kpiValue=kpiValue/1024/1024;
								BigDecimal b=new BigDecimal(kpiValue);
								kpiValue=b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
							}
							match(threshold,kpiCurrent.getKpiCode(),kpiCurrent.getObj(),kpiValue);
						}
					}
				}
			}
		}
	}
	
	
	/**
	 * 进行比较
	 * @param threshold
	 * @param kpi
	 */
	public void match(MrWarningThreshold threshold,String kpiCode,String obj,double kpiValue) {
		//查比较符
		String symbol=warningRecordDao.selectSymbol(threshold.getComparisonOperators());
		if("大于等于".equals(symbol)) {
			//大于等于
			if(Double.valueOf(kpiValue)>=threshold.getMonitoringThreshold()) {
				insertWarning(threshold,kpiCode,obj,kpiValue);
			}
		}else {
			//小于等于
			if(Double.valueOf(kpiValue)<=threshold.getMonitoringThreshold()) {
				insertWarning(threshold,kpiCode,obj,kpiValue);
			}
		}
	}

	/**
	 * 预警记录
	 * @param kpi
	 * @param threshold
	 */
	public void insertWarning(MrWarningThreshold threshold,String kpiCode,String obj,double kpiValue) {
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		//进行预警
		//System.out.println("sendWarning");

		//将数据记录到处理表
		MrWarningProcessing processing=new MrWarningProcessing();
		processing.setId(UUIDUtil.getUUId());
		processing.setCreatedTime(new Date());
		processing.setDelFlag(0);
		/*
		 * 监控指表   报警时段  预警值 比较符 实际值 监控类型 对象 通知人 方式 状态
		 */
		processing.setAlarmItem(kpiCode);
		processing.setAlarmTime(format.format(new Date()));
		processing.setWarningThreshold(threshold.getMonitoringThreshold());
		processing.setComparisonOperators(threshold.getComparisonOperators());
		processing.setActualNumerical(String.valueOf(kpiValue));
		processing.setObjType(threshold.getObjType());
		processing.setObj(obj);
		processing.setPrincipal(threshold.getPrincipalId());
		processing.setWarningType(warningRecordDao.selectWarningTypeName(threshold.getWarningType()));
		String unitName=warningRecordDao.selectUnitName(threshold.getUnitCode());
		processing.setUnit(unitName);
		//状态
		processing.setStatus("1902");

		//插入处理表
		warningRecordDao.insertProcessing(processing);
	}

}
